package com.cg.design.depthdesign.singleton;

/**
 * 基础单例（多线程）
 * <p>
 * 相同的类在多线程环境中会出错。 多线程可能会同时调用构建方法并获取多个单例类的实例。
 *
 * @author cg
 * @program LeetCode->MultiSingleton
 * @create 2022-07-18 22:01
 **/
public class MultiSingleton {

    private static MultiSingleton instance;
    public String value;

    private MultiSingleton(String value) {
        // The following code emulates slow initialization.
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        this.value = value;
    }

    public static MultiSingleton getInstance(String value) {
        if (instance == null) {
            instance = new MultiSingleton(value);
        }
        return instance;
    }

}
